package com.food.user.timedTask;

//秒 分 小时 日期 月份 星期 年

//6秒中执行一次
//    @Scheduled(cron ="*/6 * * * * ?")


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.food.user.entity.*;
import com.food.user.model.StoreFoodModel;
import com.food.user.model.StoreInfoModel;
import com.food.user.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.YearMonth;
import java.util.ArrayList;
import java.util.List;

@Component
public class SavaEs {
    @Autowired
    private StoreInfoService storeInfoService;
    @Autowired
    private DietTimeConfigService dietTimeConfigService;
    @Autowired
    private ReceiveTimeConfigService receiveTimeConfigService;
    @Autowired
    private ProductsInfoService productsInfoService;
    @Autowired
    private ProductsTypeService productsTypeService;
    @Autowired
    private ElasticsearchRestTemplate ert;
    @Autowired
    private FoodInfoService foodInfoService;
    @Autowired
    private ProductsFoodRelService productsFoodRelService;
    @Autowired
    private FoodMonthSaleService foodMonthSaleService;
    @Scheduled(cron ="0 */1 * * * ?")
    private void saveDate(){
        List<StoreInfo> list = storeInfoService.list(new QueryWrapper<StoreInfo>().eq("store_info_store_status", 1));
        for (StoreInfo storeInfo : list) {
            StoreInfoModel storeInfoModel = BeanUtil.toBean(storeInfo, StoreInfoModel.class);
            //装配用餐时间表
            List<DietTimeConfig> dietTimeConfigList = dietTimeConfigService.list(new QueryWrapper<DietTimeConfig>().eq("diet_time_config_store_id", storeInfo.getStoreInfoId()));
            storeInfoModel.setDietTimeConfig(dietTimeConfigList);
            //装配接单时间表
            ReceiveTimeConfig receiveTimeConfig = receiveTimeConfigService.getOne(new QueryWrapper<ReceiveTimeConfig>().eq("receive_time_config_store_id", storeInfo.getStoreInfoId()));
            storeInfoModel.setReceiveTimeConfig(receiveTimeConfig);
            //装配店铺类别表
            List<ProductsInfo> productsInfos = productsInfoService.list(new QueryWrapper<ProductsInfo>().eq("products_store_id", storeInfo.getStoreInfoId()));
            List<ProductsType> productsTypes = new ArrayList<>();
            for (ProductsInfo productsInfo : productsInfos) {
                ProductsType productsType = productsTypeService.getOne
                        (new QueryWrapper<ProductsType>().eq("type_id",productsInfo.getProductsTypeId())
                                .eq("type_state",1));
                if (productsType!=null){
                    productsTypes.add(productsType);
                }
            }
            storeInfoModel.setProductsTypes(productsTypes);
            //存入es
            ert.save(storeInfoModel);
        }
    }


    @Scheduled(cron ="0 */1 * * * ?")
    private void saveFoodDate(){
        List<FoodInfo> foodInfos = foodInfoService.list(new QueryWrapper<FoodInfo>().eq("food_info_state", 1));
        for (FoodInfo foodInfo : foodInfos) {
            StoreFoodModel storeFoodModel = BeanUtil.toBean(foodInfo, StoreFoodModel.class);
            //装配类别
            ProductsFoodRel relServiceOne = productsFoodRelService.getOne(new QueryWrapper<ProductsFoodRel>()
                    .eq("products_food_id", foodInfo.getFoodInfoId()));
            storeFoodModel.setTypeId(relServiceOne.getProductsId());
            //装配销量
            //装配月销量
            //获取当前年月
            YearMonth yearMonth=YearMonth.from(LocalDate.now());
            FoodMonthSale one = foodMonthSaleService
                    .getOne(new QueryWrapper<FoodMonthSale>()
                            .eq("food_month_sale_food_id", foodInfo.getFoodInfoId())
                            .eq("food_month_sale_date_str", yearMonth));
            if (one!=null){
                storeFoodModel.setFoodMonthSaleCount(one.getFoodMonthSaleCount());
            }else {
                storeFoodModel.setFoodMonthSaleCount(0);
            }
            //存入es
            ert.save(storeFoodModel);
        }
    }

}
